package com.safx.processor;

import java.util.List;

import org.apache.log4j.Logger;

import com.safx.entity.enums.ResultEnum;
import com.safx.entity.tags.ABaseTag;
import com.safx.entity.tags.Function;
import com.safx.entity.tags.TestCaseTag;
import com.safx.entity.tags.Html;
import com.safx.entity.tags.Verify;
import com.safx.entity.tags.Assert;
import com.safx.exception.AssertException;
import com.safx.selenium.SeleniumX;
import com.safx.entity.tags.TestDefinitionTag;

/**
 * 处理testcase <b> </b>
 * 
 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
 */
public class TestDefinitionProcessor {

	Logger logger = Logger.getLogger(TestDefinitionProcessor.class);

	/**
	 * 
	 * <b>遍历执行testcase对象</b>
	 * 并且把执行记录在该对象里
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-9
	 */
	public List<TestDefinitionTag> Processor(List<TestDefinitionTag> tdl) {
		for (TestDefinitionTag td : tdl) {
			SeleniumX.startSelenium();// 启动selenium sever.
			logger.info("TestDefinition start.");
			long startTime = System.currentTimeMillis();// 用于记录case花费的时间，这是开始的时间
			try {
				List<TestCaseTag> tcl = td.getTestsTag().getTestCaseTagList();
				for (TestCaseTag tc : tcl){
					tc.setResult(ResultEnum.PASS);
					List<ABaseTag> tsl = tc.getTestStepsTag().getTestSetpList(); 
					for (Object o : tsl) {// 遍历执行case的每个操作
						if (o instanceof Html) {
							Html h = (Html) o;
							h.excute();
							ResultEnum result = h.getResult(); 
							if(result.equals(ResultEnum.FAIL)){
								tc.setResult(ResultEnum.FAIL);
							}
						}else if(o instanceof Function){
							Function f = (Function) o;
							f.excute();
							ResultEnum result = f.getResult();
							if(result.equals(ResultEnum.FAIL)){
								tc.setResult(ResultEnum.FAIL);
							}
						} else if (o instanceof Verify) {
							Verify v = (Verify) o;
							v.excute();
							ResultEnum result = v.getResult();
							if(result.equals(ResultEnum.FAIL)){
								tc.setResult(ResultEnum.FAIL);
							}
						} else if (o instanceof Assert) {
							try {
								Assert a = (Assert) o;
								a.excute();
								ResultEnum result = a.getResult();
								if(result.equals(ResultEnum.FAIL)){
									tc.setResult(ResultEnum.FAIL);
								}
							} catch (AssertException e) {
								break;// 接到 AssertException 异常，当前case就中断。继续下一个case.
								// break打断整个循环，continue打断当前循环，继续下个循环
							}
						} else {
							// 未处理的标签实体
							logger.error("if you see this, please check com.adm.processor.TestCaseProcessor.Processor().");
						}
					} //for(Object o : tsl)
				} //for(TestCaseTag tc : tcl)
			} catch (Exception e) {
				logger.error(e.getMessage(), e);// 记录case导致崩溃的异常，保护后面的case继续执行。
			}
			td.setTime(System.currentTimeMillis() - startTime); // 用于记录case花费的时间，花费的时间
			logger.info("TestCase end.");
			SeleniumX.stopSelenium();// 关闭selenium sever.
		}
		return tdl;// 返回操作之后的 testDefinitionList，用于生成报告。
	}
}
